﻿using Daemon.Model;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Common;
using Daemon.Common.Cache;
using System.Net;
using System.Collections.Generic;
using Daemon.Common.Exceptions;

namespace DaemonAPI
{
    [ApiController]
    [Route("daemonApi/[controller]/[action]")]
    public class GenerateConfigController : BaseApiController<GenerateConfig, IGenerateConfigRepository>
    {
        private readonly IDaemonDistributedCache _daemonDistributedCache;

        public GenerateConfigController(IGenerateConfigRepository repository,
            IHttpContextAccessor httpContextAccessor,
            IDaemonDistributedCache daemonDistributedCache) : base(repository, httpContextAccessor)
        {
            _daemonDistributedCache = daemonDistributedCache;
        }

        [HttpPost]
        [Route("")]
        public override ResultModel<GenerateConfig, IGenerateConfigRepository> Add([FromBody] List<GenerateConfig> list)
        {
            var projectId = _daemonDistributedCache.Get<string>($"{UserId}_ProjectId");
            if (string.IsNullOrEmpty(projectId))
            {
                throw new BusinessException((int)BusinessCode.NoProjectId, "");
            }

            return new ResultModel<GenerateConfig, IGenerateConfigRepository>(HttpStatusCode.Created, _repository.AddRangeWithRelationships(list));
        }
    }
}
